package net.stogi.unicore.beyond.io.input.combofield;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import net.stogi.common.api.exception.Messages;
import net.stogi.unicore.beyond.api.model.PropertyValue;
import net.stogi.unicore.beyond.io.input.core.InputValidator;
import net.stogi.unicore.beyond.io.input.core.PropertyUtils;

public class ComboInputValidator implements InputValidator {

	@Override
	public boolean validate(String inputName, String inputValue, Map<String, PropertyValue> properties, Messages messages) {
		boolean valid = true;

		// Not empty.
		{
			boolean empty = PropertyUtils.asBoolean(properties.get("empty"));
			if (!empty && inputValue == null) {
				messages.error(inputName, "Field can not be empty");
				return false;
			}
		}
		if (inputValue != null) {
			// Is value in available value list.
			{
				List<String> values = Arrays.asList(PropertyUtils.asString(properties.get("values")).split(","));
				if (!values.isEmpty() && !values.contains(inputValue)) {
					messages.error(inputName, String.format("Field value is not in available value list %1$s", inputValue));
					valid = false;
				}
			}
			// FIXME: Simple value check.
			{
				if ("value1".equals(inputValue)) {
					messages.error(inputName, String.format("Field value can not be equlas %1$s", inputValue));
					valid = false;
				}
			}
		}

		return valid;
	}
}
